package cn.mesmile.kafka.listener;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

/**
 * @author zb
 * @date 2021/10/31 17:01
 * @Description
 */
@Component
public class KafkaListener {

    private static final String TOPIC_NAME = "hello_kafka";

    @org.springframework.kafka.annotation.KafkaListener(topics = {TOPIC_NAME},groupId = "xdlcass-test-gp")
    public void onMessage1(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic){
        // 打印出消息内容
        System.out.println("消费："+record.topic()+"--"+record.partition()+"--"+record.value());
        // 手动ack确认
        ack.acknowledge();
    }

}
